package com.quanyan.place.service.impl;

import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqFinanceMeasureInfo;
import com.quanyan.place.entity.apireq.ReqPlaceRevenueStreamVo;
import com.quanyan.place.entity.apiresp.*;
import com.quanyan.place.entity.apiresp.RespFinanceMeasureInfo;
import com.quanyan.place.entity.vo.PlaceRevenueStreamVo;
import com.quanyan.place.mapper.FinanceMeasureMapper;
import com.quanyan.place.entity.apiresp.RespPlaceRevenueStreamVo;
import com.quanyan.place.entity.vo.PlacePayItemVo;
import com.quanyan.place.mapper.*;
import com.quanyan.place.service.FinanceMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;


/**
 * Created by liuyaoxin on 2016/7/26
 */
@Service("financeMeasureService")
public class FinanceMeasureServiceImpl implements FinanceMeasureService {
    @Autowired
    FinanceMeasureMapper financeMeasureMapper;
    @Autowired
    TbBizPlaceOrderMapper tbBizPlaceOrderMapper;

    @Autowired
    TbBizPlaceMapper tbBizPlaceMapper;

    @Autowired
    TbBizPlacePayItemMapper tbBizPlacePayItemMapper;

    @Autowired
    FinanceCommonManagerMapper financeCommonManagerMapper;

    @Autowired

    TbBizPlaceFinancePlaceMapper tbBizPlaceFinancePlaceMapper;

    @Autowired
    TbBizPlaceOrderItemMapper tbBizPlaceOrderItemMapper;

    @Autowired
    TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;

    @Autowired
    TbBizPlaceBuyContractMapper tbBizPlaceBuyContractMapper;

    @Autowired
    TbAdminUserMapper tbAdminUserMapper;

    /**
     * 盈亏列表
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public PageObj<List<RespFinanceMeasureInfo>> queryFinanceDataByCategoryView(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        Integer  pageNum = reqFinanceMeasureInfo.getPageNum();
        Integer  pageSize = reqFinanceMeasureInfo.getPageSize();
        int total= financeMeasureMapper.queryFinanceDataByCategoryViewCount(reqFinanceMeasureInfo);
        reqFinanceMeasureInfo.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<RespFinanceMeasureInfo>list=financeMeasureMapper.queryFinanceDataByCategoryView(reqFinanceMeasureInfo);
        PageObj<List<RespFinanceMeasureInfo>> result = PageObj.create(total, pageNum, pageSize, list);
        return result;
    }


    /**
     * 盈亏列表导出
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public List<RespFinanceMeasureInfo> queryFinanceDataByCategoryViewReport(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        List<RespFinanceMeasureInfo>list=financeMeasureMapper.queryFinanceDataByCategoryView(reqFinanceMeasureInfo);
        return list;
    }

    /**
     * 分馆列表
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public PageObj<List<RespFinanceMeasureInfo>> queryFinanceDataByPlaceView(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        Integer  pageNum = reqFinanceMeasureInfo.getPageNum();
        Integer  pageSize = reqFinanceMeasureInfo.getPageSize();
        int total= financeMeasureMapper.queryFinanceDataByPlaceViewCount(reqFinanceMeasureInfo);
        reqFinanceMeasureInfo.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<RespFinanceMeasureInfo>list=financeMeasureMapper.queryFinanceDataByPlaceView(reqFinanceMeasureInfo);
        PageObj<List<RespFinanceMeasureInfo>> result = PageObj.create(total, pageNum, pageSize, list);
        return result;
    }

    /**
     * 分馆列表导出
     * @param reqFinanceMeasureInfo
     * @return
     */
    @Override
    public List<RespFinanceMeasureInfo> queryFinanceDataByPlaceViewReport(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        List<RespFinanceMeasureInfo>list=financeMeasureMapper.queryFinanceDataByPlaceView(reqFinanceMeasureInfo);
        return list;
    }


    @Override
    public void importFinanceDataByCategory(ReqFinanceMeasureInfo reqFinanceMeasureInfo) {
        List<RespFinanceMeasureInfo> list = financeMeasureMapper.getFinanceData(reqFinanceMeasureInfo);
        for (RespFinanceMeasureInfo respVo : list) {
            TbBizPlaceFinancePlace vo=new TbBizPlaceFinancePlace();
             vo.setCityId(respVo.getCityId());
             vo.setPlaceId(respVo.getPlaceId());
             vo.setCategoryId(respVo.getCategoryId());
             vo.setDebit(respVo.getShouldPay());
             vo.setAmount(respVo.getActuallyPay());
             vo.setEndTime(respVo.getEndTime());
             vo.setCreateTime(new Date());
            tbBizPlaceFinancePlaceMapper.insert(vo);
        }
    }

    /**
     * 场馆收入流水
     * @param reqPlaceRevenueStreamVo
     * @return
     */

    @Override
    public PageObj<List<RespPlaceRevenueStreamVo>> getPlaceRevenueStream(ReqPlaceRevenueStreamVo reqPlaceRevenueStreamVo) {
        Integer pageNum = reqPlaceRevenueStreamVo.getPageNum();
        Integer pageSize = reqPlaceRevenueStreamVo.getPageSize();
        Integer total = financeCommonManagerMapper.selectPlaceRevenueStreamCount(reqPlaceRevenueStreamVo);
        reqPlaceRevenueStreamVo.setPage(new Page(pageNum * pageSize, pageSize, total));
        List<PlaceRevenueStreamVo> placeRevenueStreamVoList = financeCommonManagerMapper.selectPlaceRevenueStream(reqPlaceRevenueStreamVo);
        List<RespPlaceRevenueStreamVo> placePlaceRevenueStreamList = new ArrayList<>();
        for (PlaceRevenueStreamVo placeRevenueStreamVo:placeRevenueStreamVoList){
            RespPlaceRevenueStreamVo revenueStreamVo = new RespPlaceRevenueStreamVo();
            revenueStreamVo.setPlaceName(placeRevenueStreamVo.getPlaceName());
            revenueStreamVo.setCityId(placeRevenueStreamVo.getCityId());
            revenueStreamVo.setPlaceOrderId(placeRevenueStreamVo.getPlaceOrderId());
            revenueStreamVo.setCategoryId(placeRevenueStreamVo.getCategoryId());
            revenueStreamVo.setCustomer(placeRevenueStreamVo.getCustomer());
            revenueStreamVo.setPayDate(placeRevenueStreamVo.getOrderTime());
            revenueStreamVo.setPlaceCode(placeRevenueStreamVo.getPlaceCode());
            revenueStreamVo.setResponsibleName(placeRevenueStreamVo.getResponsibleName());
            revenueStreamVo.setPayItems(getPlacePayItem(placeRevenueStreamVo.getPlaceOrderId()));
            BigDecimal disCountAmount = getDiscountAmount(placeRevenueStreamVo.getPlaceOrderId());//优惠后的金额
            revenueStreamVo.setDiscountAmount(disCountAmount == null ? null:disCountAmount);
            revenueStreamVo.setSalesAmount(placeRevenueStreamVo.getSalesAmount());
            revenueStreamVo.setPlaceType("场地");
            TbBizPlaceBuyContract tbBizPlaceBuyContract = getplaceBuyContract(placeRevenueStreamVo.getPlaceId());
            if (null != tbBizPlaceBuyContract){
                revenueStreamVo.setBuyCategory(tbBizPlaceBuyContract.getBuyType());//场地类型
            }
            Integer sourceType = placeRevenueStreamVo.getSourceType();
            if (sourceType.equals(1) || sourceType.equals(3)){
                revenueStreamVo.setSalesPeople("平台");
            }else{
                Integer createUid = placeRevenueStreamVo.getCreateUid();
                if (null != createUid){
                    TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(createUid);
                    if (tbAdminUser != null){
                        revenueStreamVo.setSalesPeople(tbAdminUser.getTrueName());
                    }
                }
            }
            List<TbBizPlaceOrderItem> tbBizPlaceOrderItem = getTbBizPlaceOrderItem(placeRevenueStreamVo.getPlaceOrderId());
            if (null != tbBizPlaceOrderItem && tbBizPlaceOrderItem.size() > 0) {
                for (TbBizPlaceOrderItem orderItem : tbBizPlaceOrderItem) {
                    //销售计划详情
                    RespProductUnitInfo respProductUnitInfo = getProductUnitInfo(orderItem.getUnitSalePlanId());
                    revenueStreamVo.setSalesDate(respProductUnitInfo.getDate());
                }
            }
            placePlaceRevenueStreamList.add(revenueStreamVo);
        }
        PageObj<List<RespPlaceRevenueStreamVo>> result = PageObj.create(total, pageNum, pageSize,placePlaceRevenueStreamList);
        return result;
    }

    /**
     * 获取场地类型
     * @param placeId
     * @return
     */
    public TbBizPlaceBuyContract getplaceBuyContract(Integer placeId){
        TbBizPlaceBuyContractExample tbBizPlaceBuyContractExample = new TbBizPlaceBuyContractExample();
        TbBizPlaceBuyContractExample.Criteria criteria = tbBizPlaceBuyContractExample.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        List<TbBizPlaceBuyContract> list = tbBizPlaceBuyContractMapper.selectByExample(tbBizPlaceBuyContractExample);
        return list !=null && list.size()>0 ? list.get(0):null;
    }
    @Override
    public List<RespPlaceRevenueStreamVo> getPlaceRevenueStreamExport(ReqPlaceRevenueStreamVo reqPlaceRevenueStreamVo) {
        List<PlaceRevenueStreamVo> placeRevenueStreamVoList = financeCommonManagerMapper.selectPlaceRevenueStream(reqPlaceRevenueStreamVo);
        List<RespPlaceRevenueStreamVo> placePlaceRevenueStreamList = new ArrayList<>();
        for (PlaceRevenueStreamVo placeRevenueStreamVo:placeRevenueStreamVoList){
            RespPlaceRevenueStreamVo revenueStreamVo = new RespPlaceRevenueStreamVo();
            revenueStreamVo.setPlaceName(placeRevenueStreamVo.getPlaceName());
            revenueStreamVo.setCityId(placeRevenueStreamVo.getCityId());
            revenueStreamVo.setPlaceOrderId(placeRevenueStreamVo.getPlaceOrderId());
            revenueStreamVo.setCategoryId(placeRevenueStreamVo.getCategoryId());
            revenueStreamVo.setCustomer(placeRevenueStreamVo.getCustomer());
            revenueStreamVo.setPayDate(placeRevenueStreamVo.getOrderTime());
            revenueStreamVo.setPlaceCode(placeRevenueStreamVo.getPlaceCode());
            revenueStreamVo.setResponsibleName(placeRevenueStreamVo.getResponsibleName());
            revenueStreamVo.setPayItems(getPlacePayItem(placeRevenueStreamVo.getPlaceOrderId()));
            BigDecimal disCountAmount = getDiscountAmount(placeRevenueStreamVo.getPlaceOrderId());//优惠后的金额
            revenueStreamVo.setDiscountAmount(disCountAmount == null ? null:disCountAmount);
            revenueStreamVo.setSalesAmount(placeRevenueStreamVo.getSalesAmount());
            revenueStreamVo.setPlaceType("场地");
            TbBizPlaceBuyContract tbBizPlaceBuyContract = getplaceBuyContract(placeRevenueStreamVo.getPlaceId());
            if (null != tbBizPlaceBuyContract){
                revenueStreamVo.setBuyCategory(tbBizPlaceBuyContract.getBuyType());//场地类型
            }
            Integer sourceType = placeRevenueStreamVo.getSourceType();
            if (sourceType.equals(1) || sourceType.equals(3)){
                revenueStreamVo.setSalesPeople("平台");
            }else{
                Integer createUid = placeRevenueStreamVo.getCreateUid();
                if (null != createUid){
                    TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(createUid);
                    if (tbAdminUser != null){
                        revenueStreamVo.setSalesPeople(tbAdminUser.getTrueName());
                    }
                }
            }
            List<TbBizPlaceOrderItem> tbBizPlaceOrderItem = getTbBizPlaceOrderItem(placeRevenueStreamVo.getPlaceOrderId());
            if (null != tbBizPlaceOrderItem && tbBizPlaceOrderItem.size() > 0) {
                for (TbBizPlaceOrderItem orderItem : tbBizPlaceOrderItem) {
                    //销售计划详情
                    RespProductUnitInfo respProductUnitInfo = getProductUnitInfo(orderItem.getUnitSalePlanId());
                    revenueStreamVo.setSalesDate(respProductUnitInfo.getDate());
                }
            }
            placePlaceRevenueStreamList.add(revenueStreamVo);
        }
        return placePlaceRevenueStreamList;
    }

    /**
     * 订单优惠后金额
     * @param placeOrderId
     * @return
     */
    public BigDecimal getDiscountAmount(String placeOrderId){
        Map<String,Object> map = new HashMap();
        List<String> list = new ArrayList<>();
        list.add(placeOrderId);
        map.put("placeOrderId",list);
        return financeCommonManagerMapper.getDiscountAmount(map);
    }
    /**
     * 支付方式及支付详情
     * @param placeOrderId
     * @return
     */
    public List<PlacePayItemVo> getPlacePayItem(String placeOrderId){
        //根据支付方式查询Place_order_id
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        TbBizPlacePayItemExample.Criteria criteria = tbBizPlacePayItemExample.createCriteria();
      /*  List<Integer> payType = new ArrayList<>();
        payType.add(7);//鸟蛋
        payType.add(8);//鸟壳
        criteria.andPayTypeIn(payType);*/
        criteria.andAmountTypeEqualTo((byte) 1);//收入
        criteria.andPlaceOrderIdEqualTo(placeOrderId);
        List<TbBizPlacePayItem> payItems = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        List<PlacePayItemVo> payItemList = new ArrayList<>();
        if (!payItems.isEmpty()){
            for (TbBizPlacePayItem payItem : payItems){
                PlacePayItemVo placePayItemVo = new PlacePayItemVo();
                placePayItemVo.setAmount(payItem.getAmount());
                placePayItemVo.setPayType(payItem.getPayType());
                payItemList.add(placePayItemVo);
            }
        }
        return  payItemList;

    }

    /**
     * 订单详情
     * @param placeOrderId
     * @return
     */
    private List<TbBizPlaceOrderItem> getTbBizPlaceOrderItem(String placeOrderId) {
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        TbBizPlaceOrderItemExample.Criteria tbBizPlaceOrderItemExampleCriteria = tbBizPlaceOrderItemExample.createCriteria();
        tbBizPlaceOrderItemExampleCriteria.andPlaceOrderIdEqualTo(placeOrderId);
        List<TbBizPlaceOrderItem> list = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
        return list;
    }


    /**
     * 根据销售计划id查询订单详情
     * @param id tb_biz_place_unit_sale_plan
     * @return
     */
    private RespProductUnitInfo getProductUnitInfo(Integer id) {
        TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan = tbBizPlaceUnitSalePlanMapper.selectByPrimaryKey(id);
        RespProductUnitInfo respProductUnitInfo = new RespProductUnitInfo();
        if (null != tbBizPlaceUnitSalePlan){
            respProductUnitInfo.setDate(tbBizPlaceUnitSalePlan.getDate());
        }
        return respProductUnitInfo;
    }

}
